package com.nowcoder.community.service;

import com.nowcoder.community.entity.Message;
import com.nowcoder.community.repository.MessageMapper;
import com.nowcoder.community.utils.SensitiveWordFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
@Slf4j
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    public int countUnreadMessages(int userId){
        return messageMapper.countUnreadMessages(userId, null);
    }

    public int countConversations(int userId){
        return messageMapper.countConversations(userId);
    }

    public List<Message> findConversations(int userId, int offset, int limit){
        List<Message> messages = messageMapper.selectConversations(userId, offset, limit);
        messages.forEach(msg->decodeMessage(msg));
        return messages;
    }

    public List<Message> findConversationMessages(String conversationId, int offset, int limit){
        List<Message> messages = messageMapper.selectConversationMessages(conversationId, offset, limit);
        messages.forEach(msg->decodeMessage(msg));
        return messages;
    }

    public int countConversationUnreadMessages(int userId, String conversationId){
        return messageMapper.countUnreadMessages(userId, conversationId);
    }

    public int countConversationMessages(String conversationId){
        return messageMapper.countConversationMessages(conversationId);
    }

    public int addMessage(Message message){
        encodeMessage(message);
        return messageMapper.insertMessage(message);
    }

    public int updateMessagesStatus(List<Integer> ids, int status){
        return messageMapper.updateMessagesStatus(ids, status);
    }

    public Message findMessageById(int id){
        Message message = messageMapper.selectMessageById(id);
        decodeMessage(message);
        return message;
    }

    // 系统通知
    public Message findLatestNotice(int userId, String topic){
        Message message = messageMapper.selectLatestNotice(userId, topic);
        decodeMessage(message);
        return message;
    }

    public List<Message> findNotices(int userId, String topic, int offset, int limit){
        List<Message> messages = messageMapper.selectNotices(userId, topic, offset, limit);

        messages.forEach(msg->decodeMessage(msg));
        return messages;
    }

    public int countNotices(int userId, String topic){
        return messageMapper.countNotices(userId, topic);
    }

    public int countUnreadNotices(int userId, String topic){
        return messageMapper.countUnreadNotices(userId, topic);
    }


    // 对消息内容进行转义并过滤敏感词
    private void encodeMessage(Message message){
        if (message != null)
                message.setContent(sensitiveWordFilter.filter(HtmlUtils.htmlEscape(message.getContent())));
    }
    private void decodeMessage(Message message){
        if (message != null)
            message.setContent(HtmlUtils.htmlUnescape(message.getContent()));
    }
}
